import { httpCommonClient } from "./axios";

import Selector from "../reselector";

const global = {
  /*initApp: {
    reducer: {
      next(state: any, action: any) {
        return {
          ...state,
          ...{ menus: action.payload.data.data.menus }
        };
      }
    },
    payloadCreator: () => httpCommonClient.post("/initApp"),
  },*/
  session: {
    actions: {
      setSession: {
        reducer: {
          next(state: any, action: any) {
            if (action.payload) {
              return action.payload;
            }
            return state;
          }
        },
      },
    },
    initialState: {},
    mapStateToProps: (state, ownProps = {}) => {
      return {
        SESSION: state.session
      };
    }
  },
  dictionary: {
    actions: {
      setDictionary: {
        reducer: {
          next(state: any, action: any) {
            if (action.payload) {
              return action.payload;
            }
            return state;
          }
        },
      },
      refreshDictionary: {
        reducer: {
          next(state: any, action: any) {
            if (action.payload.success) {
              return action.payload.data;
            }
            return state;
          }
        },
        payloadCreator: () => httpCommonClient.post("/getStore")
      }
    },
    initialState: [],
    mapStateToProps: (state, ownProps = {}) => {
      return {
        DICTIONARY: state.dictionary
      };
    }
  },
  menus: {
    actions: {
      setMenu: {
        reducer: {
          next(state: any, action: any) {
            if (action.payload) {
              return action.payload;
            }
            return state;
          }
        },
      },
    },
    initialState: [],
    mapStateToProps: (state, ownProps = {}) => {
      return {
        MENUS: state.menus
      };
    }
  },
  menuSelection: {
    actions: {
      selectMenu: {
        reducer: {
          next(state: any, action: any) {
            if (action.payload.pattern) {
              return action.payload.pattern;
            }
            return state;
          }
        },
        payloadCreator: a => a
      }
    },
    initialState: "",
    mapStateToProps: (state, ownProps = {}) => {
      return {
        menuSelection: state.menuSelection
      };
    }
  },
  routerParam: {
    actions: {
      setRouterParam: {
        reducer: {
          next(state: any, action: any) {
            const { pattern, param } = action.payload;
            if (pattern) {
              return { ...state, [pattern]: param };
            }
            return state;
          }
        },
        payloadCreator: a => a
      }
    },
    initialState: {},
    mapStateToProps: (state, ownProps = {}) => {
      return {
        getComponentParam: Selector.getComponentParam(state)
      };
    }
  }
};

export default global;
